#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Engine
// Code File Name: StringTools.cs
// Code File Date: 12/07/2006
//
// Code File Info: Common string tool utilities
//=============================================================================
/*
 * UPDATES
 * -------
 *              ShawnJ 5/11/2007 (0.9.0.x): Refactoring for OSS.
 */
#endregion Header

#region License
/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#endregion License

#region Includes
using System;
using System.Collections;
#endregion Includes

namespace WaveTech.GShield.Engine.Tools
{
	/// <summary>
	/// Common string tool utilities
	/// </summary>
	public static class StringTools
	{
		#region Get File Name
		/// <summary>
		/// Takes in a full path to a executable, or file, and returns
		/// just the file name.
		/// </summary>
		/// <param name="fullPath">Full Path to parse</param>
		/// <returns>File name and extension</returns>
		public static string GetFileName(string fullPath)
		{
			if (fullPath == null || fullPath.Length <= 0)
				throw new ArgumentNullException("fullPath");

			return fullPath.Substring(fullPath.LastIndexOf(char.Parse("\\")) + 1, fullPath.Length - (fullPath.LastIndexOf(char.Parse("\\")) + 1));
		}
		#endregion Get File Name

		#region Get Directory Path
		/// <summary>
		/// Takes in a full path to a executable, or file, and returns
		/// just the directory path.
		/// </summary>
		/// <param name="fullPath">Full Path to parse</param>
		/// <returns>Directory path minus filename</returns>
		public static string GetDirectoryPath(string fullPath)
		{
			if (fullPath == null || fullPath.Length <= 0)
				throw new ArgumentNullException("fullPath");

			return fullPath.Substring(0, fullPath.LastIndexOf(char.Parse("\\")));
		}
		#endregion Get Directory Path

		#region Get Directory Name
		/// <summary>
		/// Takes a full path to an executable and returns just the directory name.
		/// </summary>
		/// <param name="fullPath">Full path to parse</param>
		/// <param name="doNormalize">True if you want to Normalize (Strip out) common sub directory names.</param>
		/// <returns>Directory name</returns>
		public static string GetDirectoryName(string fullPath, bool doNormalize)
		{
			if (fullPath == null || fullPath.Length <= 0)
				throw new ArgumentNullException("fullPath");

			string temp;

			if (doNormalize)
				temp = PathCleaner.Clean(PathCleaner.StripFileName(fullPath));
			else
				temp = PathCleaner.StripFileName(fullPath);

			return temp.Substring(temp.LastIndexOf(char.Parse("\\")) + 1, temp.Length - (temp.LastIndexOf(char.Parse("\\")) + 1));
		}
		#endregion Get Directory Name

		#region Format Exception
		/// <summary>
		/// Format an exception and convert it to a string.
		/// </summary>
		/// <param name="exception"><c>Exception</c> to convert/format.</param>
		/// <returns>String representing the exception.</returns>
		public static string FormatException(Exception exception)
		{
			string strText = string.Empty;

			if (exception.Message != null)
				strText += exception.Message + Environment.NewLine;

			if (exception.Source != null)
				strText += exception.Source + Environment.NewLine;

			if (exception.StackTrace != null)
				strText += exception.StackTrace + Environment.NewLine;

			if (exception.TargetSite != null)
				strText += exception.TargetSite + Environment.NewLine;

			if (exception.Data != null)
			{
				strText += Environment.NewLine;
				foreach (DictionaryEntry de in exception.Data)
					strText += @"'" + de.Key + @"' -> '" + de.Value + @"'" +
						Environment.NewLine;
			}

			if (exception.InnerException != null)
			{
				strText += Environment.NewLine + "Inner:" + Environment.NewLine;
				if (exception.InnerException.Message != null)
					strText += exception.InnerException.Message + Environment.NewLine;

				if (exception.InnerException.Source != null)
					strText += exception.InnerException.Source + Environment.NewLine;

				if (exception.InnerException.StackTrace != null)
					strText += exception.InnerException.StackTrace + Environment.NewLine;

				if (exception.InnerException.TargetSite != null)
					strText += exception.InnerException.TargetSite.ToString();

				if (exception.InnerException.Data != null)
				{
					strText += Environment.NewLine;
					foreach (DictionaryEntry de in exception.InnerException.Data)
						strText += @"'" + de.Key + @"' -> '" + de.Value + @"'" +
							Environment.NewLine;
				}

			}

			return strText;
		}
		#endregion Format Exception
	}
}